import Vue from 'vue'
import App from './App'
import router from './router'
import store from './store/index'
import $ from 'jquery'
import AMap from 'vue-amap'
import ElementUI from 'element-ui';
import VueResource from 'vue-resource'
import publicURL from './assets/js/publicURL'
import 'element-ui/lib/theme-chalk/index.css'

Vue.use(AMap)
Vue.use(ElementUI)
Vue.use(VueResource)

/*公用接口地址*/
Vue.prototype.conURL = publicURL.conURL;
/*服务器接口地址*/
// Vue.prototype.conURL = 'https://www.cqyzb.cn/api/v1/'

Vue.http.interceptors.push((request, next) => {
  // Vue.http.headers.common.Authorization = 'Bearer ' + localStorage.getItem('token');
  Vue.http.headers.common['Accept'] = 'application/json';
  Vue.http.headers.common['Authorization'] = 'Bearer ' + localStorage.getItem('access_token');
  // continue to next interceptor
  if(store.state.canGoOn){
    store.state.canGoOn = false
    next((response) => {//在响应之后传给then之前对response进行修改和逻辑判断。对于token时候已过期的判断，就添加在此处，页面中任何一次http请求都会先调用此处方法
      let self = this
      store.state.canGoOn = true
      if (response.status != 200){
        if (response.status == 401){ //token过期
          let oldTime = localStorage.getItem('expires_in')
          let newTime = new Date().getTime()
          if (newTime > oldTime){
            $.ajax({
              type:'post',
              url:this.conURL + 'refresh',
              data:{refresh_token:localStorage.getItem('refresh_token')},
              cache:false,
              dataType:'json',
              success:function(data){
                if (data.status == 'success'){
                  localStorage.setItem('access_token',data.data.access_token)
                  localStorage.setItem('expires_in',data.data.expires_in)
                  localStorage.setItem('refresh_token',data.data.refresh_token)
                }
              }
            });
          }else {
            localStorage.setItem('access_token','')
            localStorage.setItem('expires_in','')
            localStorage.setItem('refresh_token','')
            setTimeout(() => {
              window.location.href = 'components/login/loginIndex'
            },1000)
          }
        }else if (response.status == 500){ //服务器异常，请联系管理员
          alert('服务器异常，请联系管理员')
          console.log(response,'异常')
        }else if (response.status == 429){
          alert('操作过于频繁，即将强制刷新页面')
          setTimeout(() => {
            location.reload()
          },3000)
        }
      }else {
        return response;
      }

    });
  }
});

Vue.config.productionTip = false

// 路由守卫，监听跳转，控制路由跳转动画
router.beforeEach(function (to, from, next) {
  if (from.fullPath != '/' && to.fullPath != '/'){
    store.state.TDshow = true
    store.state.TTDshow = true
  }
  next()
})

router.afterEach(function (to) {
  if (to.fullPath != '/'){
    if (localStorage.getItem('access_token') == null){
      window.location.href = 'components/login/loginIndex'
    }else {
      setTimeout(() => {
        store.state.TTDshow = false
        setTimeout(() => {
          store.state.TDshow = false
        },1000)
      },500)
    }
  }
})

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,//使用store
  components: { App },
  template: '<App/>'
})
